﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using Entities;
using Entities.Model;
using Entities.DB;
using Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Drawing;
using System.IO;

namespace TestGate
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string Hash(string ToHash)
        {
            Encoder enc = System.Text.Encoding.ASCII.GetEncoder();

            // Create a buffer large enough to hold the string
            byte[] data = new byte[ToHash.Length];
            enc.GetBytes(ToHash.ToCharArray(), 0, ToHash.Length, data, 0, true);

            // This is one implementation of the abstract class MD5.
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(data);

            return BitConverter.ToString(result).Replace("-", "").ToLower();
        }

        [WebMethod]
        public string DatabaseInsertProductsTest(string ProductName, int CurrentStock, string StockUnits, string Category, int Price, string Description, string Coment)
        {
            IProducts products = new Products();
            products.ProductID = Hash(DateTime.Now.ToString() + Category + ProductName);
            products.ProductName = ProductName;
            products.CurrentStock = CurrentStock;
            products.StockUnits = StockUnits;
            products.Category = Category;
            products.Price = Price;
            products.ProductInfo = new Product_Info();
            products.ProductInfo.Description = Description;
            products.ProductInfo.Comment = Coment;
            products.ProductInfo.Picture = null;

            IProductDBManager pdbm = new ProductDBManager();

            return pdbm.CreateProduct(products).ToString();
        }

        [WebMethod]
        public string DatabaseInsertTest(String login, string id, string username)
        {
            IUsers user = new Users();
            user.Login = login;
            user.Token = "lolhash";
            user.User_id = id;
            user.User_name = username;
            user.PostCode = "abc";
            user.Telephone = "qq";
            user.Email = "asfaf";
            user.Address = "fsfs";
            user.Country = "asfasg";
            user.FirstName = "loh";
            user.LastName = "Nevas";

            IUserDBManager udbm = new UserDBManager();

            return udbm.CreateUser(user).ToString();
        }


        [WebMethod]
        public string DatabaseUpdateTest(String login, string id, string username, string PostCode, string Address)
        {
            IUsers user = new Users();
            user.Login = login;
            user.Token = "qqlolhash";
            user.User_id = id;
            user.User_name = username;
            user.PostCode = PostCode;
            user.Telephone = "qqqq";
            user.Email = "qqasfaf";
            user.Address = Address;
            user.Country = "qqasfasg";
            user.FirstName = "loh";
            user.LastName = "Nevas";


            IUserDBManager udbm = new UserDBManager();

            return udbm.UpdateUser(user).ToString();
        }

        [WebMethod]
        public string DatabaseDeleteTest(string id)
        {
            IUserDBManager udbm = new UserDBManager();

            return udbm.DeleteUser(id).ToString();
        }

        [WebMethod]
        public string DatabaseGetByIDTest(string id)
        {
            IUserDBManager udbm = new UserDBManager();

            IUsers user = udbm.GetUserByID(id);
            string result = user.User_id + " " + user.Login + " " + user.Token + " " + user.Telephone;

            return result;
        }
        [WebMethod]
        public string DatabaseGetByNameTest(string FirstName, string LastName)
        {
            IUserDBManager udbm = new UserDBManager();

            IList<IUsers> List = udbm.GetUserByName(FirstName, LastName);

            if (List == null) return "null";
            string result = "";
            foreach(IUsers user in List) {
                result = result + " " + user.User_id + " " + user.Login + " " + user.Token + " " + user.Telephone;
            }


            return result;
        }

    }
}